package com.future.mayi.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.model.KeepGoodsModel;
import com.future.mayi.model.interfaces.OnKeepGoodItemClickListener;
import com.future.mayi.utils.HCUtils;

import java.util.ArrayList;
import java.util.List;

public class KeepGoodsListView extends LinearLayout implements View.OnClickListener {
    private List<View> viewList;
    private LayoutInflater mInflater;
    private Context mContext;
    private int width;
    private int imgHeight;

    private List<KeepGoodsModel> list;


    private OnKeepGoodItemClickListener listener;

    public KeepGoodsListView(Context context) {
        super(context);
        init(context);
    }

    public KeepGoodsListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public KeepGoodsListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        mContext = context;
        setOrientation(VERTICAL);
        mInflater = LayoutInflater.from(mContext);
        width = (CommonUtils.getScreenSize(mContext)[0] - CommonUtils.dip2px(mContext, 10 * 2)) / 4;
        imgHeight = width - CommonUtils.dip2px(mContext, 10);
    }

    public void setList(List<KeepGoodsModel> list) {
        this.list = list;

        if (list == null || list.size() == 0) {
            if (viewList != null && viewList.size() > 0)
                viewList.clear();
            removeAllViews();
            return;
        }

        int addViewSize = list.size();
        if (viewList != null && viewList.size() > 0) {
            addViewSize = addViewSize - viewList.size();
            if (addViewSize < 0) addViewSize = 0;
        }
        if (viewList == null) viewList = new ArrayList<>(2);

        LinearLayout linearLayout = null;
        if (addViewSize != 0) {
            for (int i = 0; i < addViewSize; i++) {
                if (linearLayout == null) {
                    linearLayout = new LinearLayout(mContext);
                    linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                    linearLayout.setOrientation(HORIZONTAL);
                    addView(linearLayout);
                }
                View view = mInflater.inflate(R.layout.layout_keep_goods_item_view, this, false);
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
                view.setLayoutParams(layoutParams);
                linearLayout.addView(view);
                viewList.add(view);
                if (linearLayout.getChildCount() >= 4) linearLayout = null;
            }
        }

        List<View> removeViews = new ArrayList<>();
        for (int i = 0; i < viewList.size(); i++) {
            View view = viewList.get(i);
            if (view != null && view.getParent() != null) {
                if (list.size() > i) {
                    KeepGoodsModel model = list.get(i);
                    ImageView imageView = (ImageView) view.findViewById(R.id.iv_img);
                    imageView.getLayoutParams().height = imgHeight;
                    TextView textView = (TextView) view.findViewById(R.id.tv_title);
                    if (model != null) {
                        HCUtils.loadWebImg(mContext, imageView, model.getType_img());
                        textView.setText(model.getType_name() == null ? "" : model.getType_name());
                    }
                    view.setTag(model);
                    view.setOnClickListener(this);
                } else {
                    removeView(view);
                    removeViews.add(view);
                }
            }
        }

        if (removeViews.size() > 0)
            viewList.removeAll(removeViews);

    }

    @Override
    public void onClick(View v) {
        if (v.getTag() == null || (!(v.getTag() instanceof KeepGoodsModel))) return;
        KeepGoodsModel model = (KeepGoodsModel) v.getTag();
        if (model != null && listener != null) listener.onItemClick(model);
    }

    public void setListener(OnKeepGoodItemClickListener listener) {
        this.listener = listener;
    }
}
